package cn.zyjw.controller;

import cn.zyjw.domain.SysMenu;
import cn.zyjw.model.R;
import cn.zyjw.model.RolePrivilegesParam;
import cn.zyjw.service.SysMenuService;
import cn.zyjw.service.SysRolePrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("角色配置")
public class SysRolePrivilegeController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @GetMapping("/roles_privileges")
    @ApiOperation("查询角色权限配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id")
    })
    public R<List<SysMenu>> findRolePrivilegesList(@RequestParam Long roleId) {
        List<SysMenu> menus = sysMenuService.findRolePrivilegesList(roleId);
        return R.ok(menus);
    }

    @PostMapping("/grant_privileges")
    @ApiOperation("配置角色权限保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolePrivilegesParam", value = "rolePrivilegesParam json数据")
    })
    public R<String> grantPrivileges(@RequestBody @Validated RolePrivilegesParam rolePrivilegesParam) {
        boolean isOk = sysRolePrivilegeService.grantPrivileges(rolePrivilegesParam);
        if (isOk) {
            return R.ok("配置角色权限保存成功");
        }
        return R.fail("配置角色权限保存失败");
    }
}
